<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="fn"  uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page import="kr.co.insoft.framework.config.Authorities" %>
<c:set var="CONTEXT">${pageContext.request.contextPath}</c:set>
<c:set var="cssUrl">${pageContext.request.contextPath}/resources/css</c:set>
<c:set var="imgUrl">${pageContext.request.contextPath}/resources/images</c:set>
<c:set var="jsUrl">${pageContext.request.contextPath}/resources/js</c:set>
<link rel="stylesheet" type="text/css" href="${cssUrl }/skin/ui.dynatree.css" >
<script type="text/javascript" src="${jsUrl }/jquery-ui.min.js"></script>
<script type="text/javascript" src="${jsUrl }/jquery.cookie.js"></script>
<script type="text/javascript" src="${jsUrl }/jquery.dynatree.js"></script>
<script type="text/javascript" src="${jsUrl}/contextmenu/jquery.contextMenu-custom.js"></script>
<link rel="stylesheet" type="text/css" href="${jsUrl}/contextmenu/jquery.contextMenu.css" />
<div class="col-sm-12" id="eventChainMainBody">
	<div class="col-sm-3 treeZone">
		<div id="categoryTreeZone"></div>
	</div>
	<div class="col-sm-9" id="nodeDataZone">
		<div id="contentZone"></div>
	</div>
	<div id="additionalContentZone"></div>
</div>

<script type="text/javascript">
function callingForm(formType, types, targetid) {
	Loading.show();
	$("#contentZone").fadeOut(500, function() {
		var url = "${CONTEXT}/eventchain/eventchain/lists";
		var param = {"types" : types, "targetid" : targetid};
		$.post(url, param, function(res) {
			$("#contentZone").html(res);
			$("#contentZone").fadeIn(500);
		}).fail(function() {
			bootbox.alert("사용시간이 만료되었습니다. 페이지를 리로딩합니다.", function() {
				location.reload();
			});
		}).always(function() {
			Loading.hide();
		});		
	});
}


$(document).ready(function() {
	$("#contentZone").hide();
	$("#additionalContentZone").hide();
	$("#categoryTreeZone").dynatree({
		debugLevel : 0,
		persist: true,
		fx: { height: "toggle", duration: 200 },
		autoFocus: true,
		initAjax : {
			url: "${CONTEXT}/rest/orchestration/orchestration/getEventChainTreeNonRoot.json"
		},
		onPostInit : function(isReloading, isError) {
			this.reactivate();
		},
		onCreate: function(node, span){
	    },
		onClick: function(node, event) {
        },
		onActivate : function(node) {
			node.toggleSelect();
		},
		onSelect: function(select, node) {
			var selKeys = $.map(node.tree.getSelectedNodes(), function(node){
				return node.data.child_uid;
			});
			
			//selectedItem = [];
			$.map(node.tree.getSelectedNodes(), function(node){
				//selectedItem.push(node.data.metadata);
			});
			var selRootNodes = node.tree.getSelectedNodes(true);
			var selRootKeys = $.map(selRootNodes, function(node){
				return node.data.child_uid;
			});
		},
		onDblClick: function(node, event) {			
			if ( node.data.metadata.types ) {
				console.log("metadata", node.data.metadata);
				callingForm(node.data.metadata.types, node.data.metadata.child_uid);
			} 
		},
		onKeydown: function(node, event) {
			if( $(".contextMenu:visible").length > 0 )
		          return false;
			
			switch( event.which ) {
	        case 32: // [Space]
	          $(node.span).trigger("mousedown", {
	            preventDefault: true,
	            button: 2
	            })
	          .trigger("mouseup", {
	            preventDefault: true,
	            pageX: node.span.offsetLeft,
	            pageY: node.span.offsetTop,
	            button: 2
	            });
	          return false;
	        }
		}
	});
	
	
	$(document).on("click", ".btnChainCreate", function(){
		$("#contentZone").fadeOut(500, function() {
			var url = "${CONTEXT}/eventchain/eventchain/createForm";
			var param = {"types" : "1", "targetid" : "2"};
			$.post(url, param, function(res) {
				$("#contentZone").html(res);
				$("#contentZone").fadeIn(500);
			}).fail(function() {
				bootbox.alert("사용시간이 만료되었습니다. 페이지를 리로딩합니다.", function() {
					location.reload();
				});
			}).always(function() {
				Loading.hide();
			});		
		});
	});
});
</script>